<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="addParams" :model="addParams" :rules="rules" :inline="true" label-width="68px">
          <el-form-item label-width="80px" label="链接地址" prop="longUrl">
            <el-input style="width: 500px"
                      v-model="addParams.longUrl"
                      placeholder="请输入需要缩短的链接"
                      clearable
                      size="large"
                      @keyup.enter.native="handleCreate">
              <el-button slot="append" type="primary" @click="handleCreate">缩短</el-button>
            </el-input>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="turlList">
          <el-table-column label="原网址" width="500px" align="center" prop="longUrl"/>
          <el-table-column label="生成链接" align="center" prop="url"/>
          <el-table-column label="二维码" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="qrCodeOpen(scope.row)"
              >查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
          title="微信扫描，立即访问"
          :visible.sync="qrCodeDialogVisible"
          width="18%"
          @opened="qrCodeCreate"
          :before-close="qrCodeHandleClose">
          <div style="margin-left: 10%">
            <div id="qrCode" ref="qrCode"></div>
          </div>
          <span slot="footer" class="dialog-footer"></span>
        </el-dialog>
        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">

            <el-form-item label="" prop="userId">
              <el-input v-model="form.userId" placeholder=""
              />
            </el-form-item>
            <el-form-item label="" prop="url">
              <el-input v-model="form.url" placeholder=""
              />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
  import {addTUrl, getTUrl, updateTUrl} from '@/api/turl'
  import store from '@/store'
  import QRCode from 'qrcodejs2'

  export default {
    name: 'TUrlAdd',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        isEdit: false,
        fileOpen: false,
        fileIndex: undefined,
        // 类型数据字典
        typeOptions: [],
        turlList: [],

        // 关系表类型

        // 查询参数
        addParams: {
          longUrl: "",
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          longUrl: [
            {required: true, trigger: 'blur', message: '链接不能为空'}
          ],
        },
        qrCodeDialogVisible: false
      }
    },
    created() {
      this.loading = false
    },
    methods: {
      //关闭弹框清除二维码
      qrCodeHandleClose() {
        this.qrCodeDialogVisible = false;
        this.qrCode = '';
        document.getElementById('qrCode').innerHTML = '';
      },
      //创建二维码
      qrcode(url) {
        this.qrCode = new QRCode('qrCode', {
          text: url,
          width: 200,
          height: 200
        })
      },
      qrCodeCreate() {
        let url = this.turlList[0].url
        this.$nextTick(() => {
          this.qrcode(url);
        })
      },
      //二维码显示
      qrCodeOpen(val) {
        this.qrCodeDialogVisible = true;
      },
      /** 查询参数列表 */
      addUrl() {
        this.loading = true
        this.addParams.userId = store.getters.userId
        addTUrl(this.addParams).then(response => {
          this.loading = false
          if (response.code === 200) {
            this.turlList = response.data
          }
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {

          id: undefined,
          userId: undefined,
          url: undefined,
        }
        this.resetForm('form')
      },
      // 关系
      // 文件
      /** 搜索按钮操作 */
      handleCreate() {
        this.$refs.addParams.validate(valid => {
          if (valid) {
            this.addUrl()
          }
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.resetForm('queryForm')
        this.handleCreate()
      },
      /** 修改按钮操作 */
      handleViewDetail(row) {
        this.reset()
        const ID =
          row.ID || this.ids
        getTUrl(ID).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改长链接'
          this.isEdit = true
        })
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.ID !== undefined) {
              updateTUrl(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.addUrl()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addTUrl(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.addUrl()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
    }
  }
</script>
